﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Quartz.Net.Factory;
using Newtonsoft.Json;
using Quartz.Net.Model;

namespace Quartz.Net.Web.Bootstrap.Ashx
{
    /// <summary>
    /// Quartz_handler 的摘要说明
    /// </summary>
    public class Quartz_handler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action = context.Request["action"];
            ////解决返回中文乱码问题
            context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
            context.Response.Charset = "utf-8";
            switch (action)
            {
                case "GetInfo":
                    GetInfo(context);
                    break;
                default:
                    break;
            }
        }

        public void GetInfo(HttpContext context)
        {
            resultJson result = new resultJson();
            try
            {
                List<Task_Quartz_JobTriggerModel> list = factoryClass.JobTriggerGetInfo().Where(m => m.VALID == "1").OrderByDescending(n => int.Parse(n.XH)).ToList();
                result.total = list.Count;
                result.rows = list;
                result.code = 200;
            }
            catch (Exception ex)
            {
                result.code = 500;
                result.msg = "获取信息失败！";
                factoryClass.InsertRrrorlog(ex.Message, "获取信息失败!", LogsStatus.Fatal);
            }
            finally
            {
                context.Response.Write(JsonConvert.SerializeObject(result));
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

    public class resultJson
    {
        public int code { get; set; }
        public string msg { get; set; }
        public object data { get; set; }
        public object total { get; set; }
        public object rows { get; set; }

    }
}